package com.atguigu.yygh.controller;

import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.result.Result;
import com.atguigu.yygh.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zr
 * @date 2021/12/10 12:51
 */
@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Resource
    private DictService dictService;

    /**
     * 根据数据id查询子数据列表
     *
     * @param parentId
     * @return
     */
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{parentId}")
    public Result findChildData(@PathVariable long parentId) {
        List<Dict> dictList = dictService.findChildDataByParentId(parentId);
        return Result.ok(dictList);
    }

    /**
     * 导出Excel数据
     *
     * @param response
     */
    @ApiOperation(value = "导出")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    /**
     * @param file
     * @return
     */
    @ApiOperation(value = "导入")
    @PostMapping("importData")
    public Result importData(MultipartFile file) {
        dictService.importData(file);
        return Result.ok();
    }

    @ApiOperation(value = "根据dictCode获取下级数据节点")
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode) {
        List<Dict> dictList = dictService.findByDictCode(dictCode);
        return Result.ok(dictList);
    }

    @ApiOperation(value = "根据dictcode和value查询医院等级")
    @GetMapping("getName/{dictCode}/{value}")
    public String getDictName(@PathVariable String dictCode, @PathVariable String value) {
        String dictName = dictService.getDictName(dictCode, value);
        return dictName;
    }

    @ApiOperation(value = "根据value查询医院等级")
    @GetMapping("getName/{value}")
    public String getDictName(@PathVariable String value) {
        String dictName = dictService.getDictName("", value);
        return dictName;
    }

}
